import gradio as gr


# 更新查看信息选项
def update_option(analysis:str, cs:list[str], nodes:list[str], four:list[str], *_):
    # 控制显隐
    vas = [False,False,False,False]
    dc_vs=[]
    four_vs=[]
    match analysis:
        case '瞬态分析': 
            choices = [f'v({node})' for node in nodes]+[f'I({c})' for c in cs if not c.startswith(('V','X'))]
            vas[0]=True
        case '交流分析': 
            choices = [f'ph(v({node}))' for node in nodes]+[f'db(v({node}))' for node in nodes]
            vas[1]=True
        case '直流扫描': 
            choices = [f'v({node})' for node in nodes]
            dc_vs = [c for c in cs if c.startswith('V')]
            vas[2]=True
        case '快速傅里叶': 
            four_vs=[f'v({node})' for node in nodes]
            choices=[]
            for fv in four:
                for tp in ('mag','ph','re','im'): # 幅度谱, 相位谱, 实部, 虚部
                    choices.append(f'{tp}({fv})')
            vas[3]=True
        case _: 
            raise ValueError(f'analysis-{analysis} 分析选项错误')
    return *[gr.update(visible=v) for v in vas], *[gr.update(visible=v) for v in vas], \
        gr.Dropdown(choices=choices, value=[]), gr.Dropdown(choices=nodes),\
        gr.Dropdown(choices=dc_vs, value=None), gr.Dropdown(choices=four_vs, value=[])

def update_fft(four:list[str]):
    choices=[]
    for fv in four:
        for tp in ('mag','ph','re','im'): # 幅度谱, 相位谱, 实部, 虚部
            choices.append(f'{tp}({fv})')
    return gr.Dropdown(choices=choices, value=[])